package com.weim.weim.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 微信小程序工具类
 */
@Slf4j
public class WxMiniappUtil {
    public static final String JSCODE_2_SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session";
    public static final String GET_ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";
    public static final String CODE = "code";
    public static final String ACCESS_TOKEN = "access_token";
    public static final String AUTHORIZATION_CODE = "authorization_code";
    public static final String CLIENT_CREDENTIAL = "client_credential";
    public static final String GET_PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    public static final String APPID = "wxaf9558eccf15e951";
    public static final String SECRET = "08d17b1da882d618d950bc6cee495496";


    public static HashMap<String, String> jscode2session(String wxSingCode) {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("appid", APPID);
        paramsMap.put("secret", SECRET);
        paramsMap.put("js_code", wxSingCode);
        paramsMap.put("grant_type", AUTHORIZATION_CODE);
        String jscode2sessionResult = null;
        try {
            jscode2sessionResult = HttpClientUtils.doGet(JSCODE_2_SESSION_URL, paramsMap);
            log.info("查询小程序openId结果：{} appId：{}", jscode2sessionResult, APPID);
        } catch (Exception e) {
            log.error("获取微信openId异常，paramsMap：{};e:", paramsMap, e);
            return null;
        }
        return JSON.parseObject(jscode2sessionResult, HashMap.class);
    }

    public static String getAccessToken() {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("appid", APPID);
        paramsMap.put("secret", SECRET);
        paramsMap.put("grant_type", CLIENT_CREDENTIAL);
        String accessTokenResult;
        try {
            accessTokenResult = HttpClientUtils.doGet(GET_ACCESS_TOKEN, paramsMap);
            log.info("查询小程序accessToken结果：{}", accessTokenResult);
        } catch (Exception e) {
            log.error("查询小程序accessToken异常，appId：{};e:", APPID, e);
            return null;
        }
        HashMap<String, String> accessTokenResultMap = JSON.parseObject(accessTokenResult, HashMap.class);
        if (null == accessTokenResultMap || !accessTokenResultMap.containsKey(ACCESS_TOKEN)) {
            return null;
        }
        return accessTokenResultMap.get(ACCESS_TOKEN);
    }

    public static JSONObject getWxPhone(String wxPhoneCode, String accessToken) {
        Map<String, String> phoneParamsMap = new HashMap<>();
        phoneParamsMap.put(CODE, wxPhoneCode);
        String phoneResult = HttpClientUtils.doPostJson(GET_PHONE_URL + accessToken, JSON.toJSONString(phoneParamsMap), new HashMap<>());
        log.info("查询小程序phoneResult结果：{}", phoneResult);
        return JSON.parseObject(phoneResult);
    }
}
